using System;
using System.Data;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace Helpers
{
    public class PageNumberHelper
    {
        public static List<PageItem> GetPages(int TotalPages, int CurrentPage, int maximumPagesInList)
        {
            if (TotalPages <= 1)
            {
                return null;
            }

            List<PageItem> pages;
            if (maximumPagesInList > 0)
            {
                pages = new List<PageItem>(maximumPagesInList + 1);
            }
            else
            {
                pages = new List<PageItem>(TotalPages);
            }

            int startPage;
            int endPage;
            if (TotalPages <= maximumPagesInList)
            {
                startPage = 1;
                endPage = TotalPages;
            }
            else
            {
                startPage = CurrentPage - (maximumPagesInList / 2);

                endPage = CurrentPage + (maximumPagesInList / 2);

                if (endPage < maximumPagesInList)
                {
                    endPage = maximumPagesInList;
                }

                if (endPage > TotalPages)
                {
                    endPage = TotalPages;
                    startPage = TotalPages - maximumPagesInList;
                }

                if (startPage < 1)
                {
                    startPage = 1;
                }
            }

            // adding first page if needed
            if (startPage > 1)
            {
                PageItem firstPage = new PageItem(1);
                pages.Add(firstPage);
            }

            // adding in-between items
            for (int i = startPage; i <= endPage; i++)
            {
                PageItem myPage = new PageItem(i);
                if (i == CurrentPage)
                {
                    myPage.IsCurrentPage = true;
                }
                pages.Add(myPage);
            }

            // adding last page if needed
            if (endPage < TotalPages)
            {
                PageItem lastPage = new PageItem(TotalPages);
                pages.Add(lastPage);
            }

            return pages;
        }
    }

    public class PageItem
    {
        public PageItem(int itemText)
        {
            ItemText = itemText;
        }

        private int _ItemText;
        public int ItemText
        {
            get
            {
                return _ItemText;
            }
            set
            {
                _ItemText = value;
            }
        }

        private bool _isCurrentPage;
        public bool IsCurrentPage
        {
            get
            {
                return _isCurrentPage;
            }
            set
            {
                _isCurrentPage = value;
            }
        }
    }
}